var express = require('express');
var router = express.Router();
//引入数据库包
var db = require("./db.js");


/*查看详情*/
router.get('/view/:id', function (req, res) {
    var id = req.params.id;
    console.log(id);
    db.query("select * from info where id=" + id, function (err, rows) {
        if (err) {
            res.send("修改跳转失败"+err)
        } else {
            res.render("infoAdmin/view", {datas: rows});       //直接跳转
        }
    });
});

/**
 * 查询列表页
 */
router.get('/', function (req, res, next) {
    db.query('select * from info', function (err, rows) {
        console.log(rows);
        if (err) {
            res.render('infoAdmin/infoAdmin', {title: 'nodeJsDemo', datas: []});
        } else {
            res.render('infoAdmin/infoAdmin', {title: 'nodeJsDemo', datas: rows});
        }
    })
});
/**
 * 新增页面跳转
 */

router.get('/add', function (req, res) {
    res.render('infoAdmin/add');
});
router.post('/add', function (req, res) {
    var title = req.body.title;
    var info = req.body.info;
    var information = req.body.information;
/*这里的双引号内嵌套单引号的原因是为了字符串存储*/
    db.query("insert into info(title,info,information) values('" + title + "','" + info + "','"+information+"')", function (err, rows) {
        if (err) {
            res.send("错了傻吊:"+err)
        } else {
            res.redirect('/infoAdmin');
        }
    })
});

/**
 * 删
 */
router.get('/del/:id', function (req, res) {
    var id = req.params.id;
    db.query("delete from info where id=" + id, function (err, rows) {
        if (err) {
            res.send("没删除掉："+err)
        } else {
            res.redirect('/infoAdmin')
        }
    });
});
/**
 * 修改
 */
router.get('/toUpdate/:id', function (req, res) {
    var id = req.params.id;
    console.log(id);
    db.query("select * from info where id=" + id, function (err, rows) {
        if (err) {
            res.send("修改跳转失败"+err)
        } else {
            res.render("infoAdmin/update", {datas: rows});       //直接跳转
        }
    });
});
router.post('/update', function (req, res) {
    var id = req.body.id;
    var name = req.body.name;
    var age = req.body.age;
    var professional = req.body.professional;
    db.query("update info set title='" + title + "',info='" + info + "',information='" + information + "' where id=" + id, function (err, rows) {
        if (err) {
            res.send("修改失败了哈哈哈："+err)
        } else {
            res.redirect('/infoAdmin');
        }
    });
});
module.exports = router;